''' <summary>
''' This is a game component that implements IUpdateable.
''' </summary>
Public Class TitleScreen
    Inherits Microsoft.Xna.Framework.DrawableGameComponent
    Implements IGameProto
    Private spriteBatch As SpriteBatch
    Private WithEvents Level As GameLevel
    Private cLocation As Vector3
    Private LevelData As GameItems
    Private Parent As Game1
    Public Property Effect As BasicEffect Implements IGameProto.Effect
    Private mMove As Single
    Private mQuad As VertexPositionTexture()
    Private mEffect As BasicEffect
    Private mCheck As Texture2D
    Private mTitle As Texture2D
    Private mStart As Texture2D
    Private mEnd As Texture2D
    Private mTCord As Single = 0
    Private mLSize As Single = 1.5
    Private mStartButton As Rectangle
    Private mEndButton As Rectangle
    Private mloaded As Boolean = False

    Public Event Start()
    ' ////////////////////////////////////////////////////////////////
    Public ReadOnly Property CameraPosition As Vector3 Implements IGameProto.CameraPosition
        Get
            Return cLocation
        End Get
    End Property
    ' ////////////////////////////////////////////////////////////////
    ' Unit size property
    Public ReadOnly Property UnitSize As Single Implements IGameProto.UnitSize
        Get
            Return LevelData.UnitSize
        End Get
    End Property

    Public Sub New(ByVal game As Game)
        MyBase.New(game)
        Parent = CType(game, Game1)
        ' TODO: Construct any child components here
        Level = New GameLevel(game, Me)
        Level.DrawOrder = 2
        game.Components.Add(Level)
    End Sub

    Protected Overrides Sub LoadContent()
        ' Load the level information
        LevelData = New GameItems(Game)
        LevelData.Level = 0
        LevelData.SpeedMultiplier = 0.5
        mMove = CSng(LevelData.UnitSize * 0.01)
        ' Load the level and set up the paliminary varibales in the game items
        Level.RowCount = 30
        Level.LoadLevel(LevelData.FileName)
        ' Set up the camera
        cLocation = New Vector3(CSng(LevelData.UnitSize * 2.5), LevelData.UnitSize * 3, 0)
        Parent.Camera.Position = cLocation
        Parent.Camera.LookAt = New Vector3(CSng(LevelData.UnitSize * 2.5), -LevelData.UnitSize * 3, LevelData.UnitSize * 8)
        Parent.Camera.ViewNear = LevelData.UnitSize
        Parent.Camera.ViewFar = LevelData.UnitSize * Level.RowCount
        ' Set up the renderer
        Effect = New BasicEffect(GraphicsDevice)
        Effect.World = Matrix.Identity
        Effect.FogEnabled = True
        Effect.FogColor = New Vector3(0, 0, 0)
        Effect.FogEnd = LevelData.UnitSize * Level.RowCount
        Effect.FogStart = LevelData.UnitSize * 4
        Effect.View = Parent.Camera.View
        Effect.Projection = Parent.Camera.Projection
        Effect.VertexColorEnabled = True

        ' Loads the title gfx
        mTitle = Game.Content.Load(Of Texture2D)("title")
        mStart = Game.Content.Load(Of Texture2D)("startbutton")
        mEnd = Game.Content.Load(Of Texture2D)("endbutton")
        mStartButton = New Rectangle(CInt(GraphicsDevice.Viewport.Width / 2.0 - mStart.Width / 2.0), GraphicsDevice.Viewport.Height, mStart.Width, mStart.Height)
        mEndButton = New Rectangle(CInt(GraphicsDevice.Viewport.Width / 2.0 - mEnd.Width / 2.0), GraphicsDevice.Viewport.Height, mEnd.Width, mEnd.Height)
        ' Scale the ball from Size 1 to our scale.
        Dim Scaler As Single = CSng(LevelData.UnitSize * 0.3)

        ' create the checker thing
        mQuad = New VertexPositionTexture(3) {}
        mQuad(0) = New VertexPositionTexture()
        mQuad(0).Position = New Vector3(1, -1, 1)
        mQuad(0).TextureCoordinate = New Vector2(15, 0)
        mQuad(1) = New VertexPositionTexture()
        mQuad(1).Position = New Vector3(1, 1, 1)
        mQuad(1).TextureCoordinate = New Vector2(15, 15)
        mQuad(2) = New VertexPositionTexture()
        mQuad(2).Position = New Vector3(-1, -1, 1)
        mQuad(2).TextureCoordinate = New Vector2(0, 0)
        mQuad(3) = New VertexPositionTexture()
        mQuad(3).Position = New Vector3(-1, 1, 1)
        mQuad(3).TextureCoordinate = New Vector2(0, 15)

        mCheck = Game.Content.Load(Of Texture2D)("checks")
        mEffect = New BasicEffect(GraphicsDevice)
        mEffect.TextureEnabled = True
        mEffect.World = Matrix.Identity
        mEffect.View = Matrix.CreateLookAt(Vector3.Zero, New Vector3(0, 0, 1), Vector3.Up)
        mEffect.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, GraphicsDevice.Viewport.AspectRatio, 0.1, 10)
        mEffect.Texture = mCheck
        mEffect.TextureEnabled = True
        spriteBatch = New SpriteBatch(GraphicsDevice)
        mloaded = True
        MyBase.LoadContent()
    End Sub

    Protected Overrides Sub UnloadContent()
        Game.Components.Remove(Level)
        Level = Nothing
        Effect = Nothing
        mQuad = Nothing
        mEffect = Nothing
        mloaded = False
        MyBase.UnloadContent()
    End Sub

    Public Overrides Sub Draw(gameTime As Microsoft.Xna.Framework.GameTime)
        mEffect.CurrentTechnique.Passes(0).Apply()
        Dim States As RenderStates = New RenderStates
        RecordRenderState(States, GraphicsDevice)
        GraphicsDevice.BlendState = BlendState.AlphaBlend
        GraphicsDevice.DepthStencilState = DepthStencilState.None
        Game.GraphicsDevice.DrawUserPrimitives(Of VertexPositionTexture)(PrimitiveType.TriangleStrip, mQuad, 0, 2)

        spriteBatch.Begin()
        spriteBatch.Draw(mTitle, New Rectangle(CInt(GraphicsDevice.Viewport.Width / 2) - CInt(mTitle.Width * mLSize / 2), 20, CInt(mTitle.Width * mLSize), CInt(mTitle.Height * mLSize)), Color.White)
        spriteBatch.Draw(mStart, mStartButton, Color.White)
        spriteBatch.Draw(mEnd, mEndButton, Color.White)
        spriteBatch.End()

        RestoreRenderState(States, GraphicsDevice)
        MyBase.Draw(gameTime)
    End Sub

    Private Sub Level_EndOfLevel() Handles Level.EndOfLevel
        cLocation.Z = 0
    End Sub

    Public Overrides Sub Initialize()
        ' TODO: Add your initialization code here
        MyBase.Initialize()
    End Sub

    Public Overrides Sub Update(ByVal gameTime As GameTime)
        Dim blnEnded As Boolean = False
        If (mloaded) Then
            ' Move the level ground
            cLocation.X -= mMove
            If (cLocation.X <= LevelData.UnitSize / 2.0) Then
                mMove = -mMove
                cLocation.X = CSng(LevelData.UnitSize / 2.0)
            End If
            If (cLocation.X >= LevelData.UnitSize * 5 - LevelData.UnitSize / 2.0) Then
                mMove = -mMove
                cLocation.X = CSng(LevelData.UnitSize * 5 - LevelData.UnitSize / 2.0)
            End If
            cLocation.Z += LevelData.Speed * LevelData.SpeedMultiplier
            Parent.Camera.Position = cLocation
            Parent.Camera.LookAt = New Vector3(CSng(LevelData.UnitSize * 2.5 + (cLocation.X - CSng(LevelData.UnitSize * 2.5)) / 1.2), -CSng(LevelData.UnitSize * 1.5), CSng(cLocation.Z + LevelData.UnitSize * 6 + LevelData.CameraSpeedIncrement / 3.0))
            Effect.View = Parent.Camera.View


            ' Move the main logo first
            If (mLSize > 0.7) Then
                mLSize -= CSng(0.08)
            Else
                If (mStartButton.Y <= 260) Then
                    mStartButton.Y = 260
                Else
                    mStartButton.Y -= 20
                End If
                If (mStartButton.Y < 300) Then
                    If (mEndButton.Y <= 360) Then
                        mEndButton.Y = 360
                        blnEnded = True
                    Else
                        mEndButton.Y -= 20
                    End If
                End If
            End If

            If (blnEnded) Then
                Dim touchCollection As TouchCollection = TouchPanel.GetState()
                For Each TouchLocation In touchCollection
                    If (IsTouching(TouchLocation, mStartButton)) Then
                        RaiseEvent Start()
                    End If
                    If (IsTouching(TouchLocation, mEndButton)) Then
                        Me.Parent.Exit()
                    End If
                Next
            End If

            ' Now move the super imposed quad
            mTCord = CSng(mTCord + 0.05)
            If (mTCord >= 1) Then
                mTCord = 0
            End If
            If (Not mQuad Is Nothing) Then
                For intI As Integer = 0 To mQuad.Count - 1
                    If (mQuad(intI).TextureCoordinate.X >= 15) Then
                        mQuad(intI).TextureCoordinate.X = 15 + mTCord
                    Else
                        mQuad(intI).TextureCoordinate.X = mTCord
                    End If
                Next
            End If
        End If
        MyBase.Update(gameTime)
    End Sub
    Private Function IsTouching(ByRef TouchLocation As TouchLocation, ByRef rect As Rectangle) As Boolean
        Return rect.Contains(New Point(CInt(TouchLocation.Position.X), CInt(TouchLocation.Position.Y)))
    End Function
End Class
